<?php
defined('BME_INSERTION') or die('Error code: '.BME_INVALID_INSERT_ERROR);
define('BME_ROOT',"/var/www/abord/");
require_once(BME_ROOT."core/etc.php");

if ( !class_exists( "BME_base" ) ):

class BME_base {
	
	var $_Class_name;	// str	Class name
	var $_srv_root;		// str	Path to the ABORD root
	var $_web_root;		// str	Address to ABORD site's root
	var $root;		// str	Alias of $_srv_root (for compatibility with dictionaries)
	var $_MySQL;		// obj	MySQL connection
	var $_system;		// str	Name of the System
	var $DB_name;		// str	Database name on MySQL
	var $conf;		// arr	Array of strings: Some configuration vars
	var $tmp_path;		// str	Path to temporal directory
	var $wget_extra;	// str	Extra options for Wget
	
	/**
	 * bme_base new bme_base(void);
	 * @return bme_base (obj).
	 */
	function __constructor() {
		$this->Class_name = get_class($this);
		$this->_srv_root = BME_SITE;
		$this->_web_root = BME_ROOT;
		$this->root =& $this->_srv_root;
		$this->tmp_path  = BME_TMP_PATH;
		$this->_system   = BME_NAME;
		$this->DB_name   = BME_DB_NAME;
		$this->wget_extra = "";
		$this->conf = array();
	}
	function __construct(){$this->__constructor();}
	// Configuration
	/**
	 * void conf_read_file(str file, str tok);
	 */
	function conf_read_file($file,$tok) {
		assert('is_string($file)');
		assert('is_string($tok)');
		assert("file_exists('$file')");
		$fp = fopen($file,"r");
		$i=0;
		while ( $ln=fgets($fp,2048) ):
			$i++;
			if( eregi("^[\t ]*#.*",$ln) ):
				continue;
			elseif( eregi("^[\t ]*\n$",$ln) ):
				continue;
			elseif( eregi("^[\t ]*include[ \t]+(.*)\n$",$ln,$reg) ):
				$this->conf_read_file(BME_ROOT.trim($reg[1]),$tok);
			elseif( eregi("^[\t ]*/(.*)\n$",$ln,$reg) ):
				if ( $this->conf_var_exists("$tok:".$last_var) ):
					$this->conf_set_var($tok, $last_var, " ".$reg[1], true);
				endif;
			elseif( eregi("^[\t ]*([^=]+)+[\t ]*=[\t ]*(.*)\n$",$ln,$reg) ):
				$last_var = trim($reg[1]);
				$this->conf_set_var($tok, $last_var, $reg[2]);
			else:
				$this->error("I can't parse line <b>$ln</b> on <b>$file:$i</b>.");
			endif;
		endwhile;
		return;
	}
	/**
	 * bool conf_var_exists(str k);
	 */
	function conf_var_exists($k) {
		assert('is_string($k)');
		if(isset($this->conf[$k])) return true;
		else return false;
	}
	/**
	 * str conf_get_var(str k);
	 */
	function conf_get_var($k) {
		assert('is_string($k)');
		assert('isset($this->conf["'.$k.'"])');
		return $this->conf[$k];
	}
	/**
	 * void conf_set_var(str tok, str k, str i[, bool add]);
	 */
	function conf_set_var($tok,$k,$i,$add=false) {
		assert('is_string($tok)');
		assert('is_string($k)');
		while ( ereg("({{{"."([[:alnum:]_\-\. ]+)"."}}})",$i,$reg) ):
			$i = str_replace($reg[1],$this->conf_get_var("$tok:".$reg[2]),$i);
		endwhile;
		while ( ereg("({{"."([[:alnum:]_\-\. ]+)"."}})",$i,$reg) ):
			isset($this->{$reg[2]}) or $this->error("I can't find the <b>{$reg[2]}</b> var.");
			$i = str_replace($reg[1],$this->{$reg[2]},$i);
		endwhile;
		if ( $add ) {
			assert('isset($this->conf["'."$tok:$k".'"])');
			$this->conf["$tok:$k"].= $i;
		} else {
			assert('!isset($this->conf["'."$tok:$k".'"])');
			$this->conf["$tok:$k"] = $i;
		}
		return;
	}
	// MySQL
	/**
	 * bool openDB(void);
	 * @return bool: true on success, dies otherwise.
	 */
	function openDB() {
		$this->_MySQL = mysql_connect("localhost","usr","xxxx") or $this->error("MySQL says: ".mysql_error());
		mysql_select_db($this->DB_name,$this->_MySQL) or $this->error("MySQL says: ".mysql_error());
		return true;
	}
	/**
	 * void closeDB(void);
	 */
	function closeDB() {
		@mysql_close($this->_MySQL);
		return;
	}
	/**
	 * obj dbquery(str sql);
	 * @param str sql: The SQL sentence.
	 * @return obj: MySQL resource 'result'.
	 */
	function DBquery($sql) {
		assert('is_string($sql)');
		($res=mysql_query($sql,$this->_MySQL)) or $this->error("<b>MySQL says:</b> ".mysql_error()." <b>on statment</b> $sql");
		return $res;
	}
	/**
	 * mix dbfetch_assoc(obj res);
	 * @param obj res: MySQL resource 'result'.
	 * @return mix: An array with the result if exists, false otherwise.
	 */
	function DBfetch_assoc($res) {
		return mysql_fetch_assoc($res);
	}
	/**
	 * str dbescape(str str);
	 * @param str str: String to escape.
	 * @return str: Escaped string.
	 */
	function DBescape($str) {
		assert('is_string($str)');
		return mysql_real_escape_string($str,$this->_MySQL);
	}
	// XML
	/**
	 * mix xml_gettagscontent(str tag, str content);
	 * @param str tag: Tags name (case insensitive).
	 * @param str content: XML content.
	 * @return mix: All the texts (str) inside tag's open and tag's close (must be explicitly closed).  If not found, returns NULL.
	 */
	function xml_getTagsContent($tag,$content) {
		assert('is_string($tag)');
		assert('is_string($content)');
		$regexp = "<{$tag}[^>]*>(.*)<{$tag}[^>]*/>";
		if ( eregi($regexp, $content, $regs) ):
			return $regs[1];
		else:
			return NULL;
		endif;
	}
	// General
	/**
	 * void error(str msg[, str level]);
	 * @param str msg: The message to show.
	 * @param str level (optional): Prints error and exits if 'fatal', prints error otherwise. 'fatal' by default.
	 */
	function error($msg,$level="fatal") {
		assert('is_string($msg)');
		assert('is_string($level)');
		echo "<hr/>
		<b>".get_class($this)." error:</b> <br/>
		$msg<br/>
		If you think it's wrong, please contact the <a href='mailto:".BME_SITEMASTER_MAIL."'>site master</a>.
		<hr/>\n";
		if ( $level=='fatal' ) die();
	}
	
}
endif;

?>
